package com.xinqing.test.algorithm.k8s.forecast;

import com.alibaba.fastjson.JSON;
import com.feidee.fdcommon.authorization.AuthorizationVerify;
import com.feidee.fdcommon.authorization.model.AuthInfoModel;
import com.feidee.fdcommon.configuration.CustomConfiguration;
import com.feidee.fdcommon.model.StringResponse;
import com.feidee.fdcommon.util.HttpUtil;
import com.xinqing.test.modle.algorithm.ForecastParam;
import org.apache.http.Consts;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author:CHQ
 * @Date:2020/9/29 17:03
 * @Description
 */
public class ST63e3ad8ee702c964 {
    public static void main(String args[]) throws Exception {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(d);


    /*   String data="{\"busId\":\"ad_ctr\"," +
                "\"features\":{\"n1\":\"1\",\"n2\":\"5\",\"n3\":\"11\",\"n4\":\"0\",\"n5\":\"0\",\"n6\":\"2\",\"n7\":\"9\",\"n8\":\"11\"," +
                "\"n9\":\"0\",\"u31\":\"1\",\"us1\":\"1\",\"us0\":\"68\",\"us3\":\"6000\",\"t1\":\"2020-09-29 17:23:27\"}," +
                "\"models\":[\"693ba772fab3a1d8\",\"951c327c56779183\"]," +
                "\"multiFeatures\":{\"97516\":{\"u32\":\"1\",\"us2\":\"0.0147\",\"us4\":\"57\",\"u37\":\"0\",\"u36\":\"0\",\"u39\":\"1\",\"u17\":\"0\",\"u38\":\"249\"}," +
                "\"top\":\"0\","+
                "\"traceId\":\"990a790a29f9e8e1d6335693ebd8a70639ba691ef4735c761554171a6fed822afcfe3475eb3dce68f890e69d73268796\"}";*/

        ForecastParam forecastParam = new ForecastParam();
        forecastParam.setBusId("ad_wdl");


        Map<String, Object> featuresMap = new HashMap<>();
        featuresMap.put("u50","0");
        featuresMap.put("u53","77");
        featuresMap.put("us0","346");
        featuresMap.put("us3","-1");
        featuresMap.put("us2","0.0000");
        featuresMap.put("u54","0");
        featuresMap.put("u58","3.0000");
        featuresMap.put("u17","0.0023");
        featuresMap.put("u38","3");
        featuresMap.put("u0","14");
        featuresMap.put("u3","14");
        featuresMap.put("u7","1");
        featuresMap.put("u8","3");
        featuresMap.put("c1","Android");
        featuresMap.put("c2","8.9.51");
        featuresMap.put("c5","cardniu");
        featuresMap.put("u42","38.5000");
        featuresMap.put("c6","miui");
        featuresMap.put("u41","14");
        featuresMap.put("u44","0");
        featuresMap.put("u43","50");
        featuresMap.put("u46","12.7778");
        featuresMap.put("u24","14");
        featuresMap.put("u45","2");
        featuresMap.put("u48","0");
        featuresMap.put("u47","77");
        featuresMap.put("u29","14");
        featuresMap.put("t1",time);
        forecastParam.setFeatures(featuresMap);
       // "\"multiFeatures\":{\"97516\":{\"u32\":\"1\",\"us2\":\"0.0147\",\"us4\":\"57\",\"u37\":\"0\",\"u36\":\"0\",\"u39\":\"1\",
        // \"u17\":\"0\",\"u38\":\"249\"}," +
        HashMap<String, Map<String, Object>> multiFeaturesMap = new HashMap<>();
        HashMap<String, Object> map = new HashMap<>();
        map.put("c3","31");
        map.put("c4","10");
        map.put("c7","KNSYFW");
        map.put("c0","39674");
        multiFeaturesMap.put("91376",map);
        forecastParam.setMultiFeatures(multiFeaturesMap);
        //"\"models\":[\"693ba772fab3a1d8\",\"951c327c56779183\"]," +
    //    forecastParam.setModels(new String[]{"aa1435d0024cacf1","1d17ee93a0132f02"});
        forecastParam.setModels(new String[]{"eccdbc899962ec5e","2415a3bb27840765"});
        forecastParam.setTop(0);

        String data = JSON.toJSONString(forecastParam);

        //分配的AccessKeyId
        String accessKeyId = "data_team";
        //分配的AccessSecret
        String accessSecret = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJ6+UYiQtt2OYfyfBF8e12kjd6nnF0Zl1s23f7l+eSl07xQxoxACSXyO3aq/tF7g0IiQZev4SF7ZA/oAOIYlYo0CAwEAAQ==";

       // String url = "http://10.201.7.239:8063/sml/forecast/realTimes";
        String url = "https://sml.suishoudata.com/model/63e3ad8ee702c964";

        //调用鉴权工具包计算鉴权信息,data不存则传空
        AuthInfoModel authInfo = AuthorizationVerify.getAuthInfo(url, data, accessKeyId, accessSecret);
        //获取GMT时间+
        String date = authInfo.getDate();
        //获取加密之后的data，若传入data为空，则密文也为空
        String ciphertext = authInfo.getCiphertext();
        //获取认证信息
        String authorization = authInfo.getAuthorization();

        //构造请求
        HttpPost method = null;
        try {
            method = new HttpPost(url);
            method.setHeader("Date", date);
            method.setHeader("Authorization", authorization);
            method.setHeader("AuthVersion", "2");
            if (ciphertext != null) {
                //加密之后data通过http body传输
                StringEntity stringEntity = new StringEntity(ciphertext, Consts.UTF_8);
                stringEntity.setContentEncoding("UTF-8");
                stringEntity.setContentType("application/json");
                method.setEntity(stringEntity);
            }
            CustomConfiguration.setString("http.connection.socket.timeout", "50000");
            StringResponse response = HttpUtil.post(method);
            System.out.println("rslt:" + response.getResponseBody());
        } finally {
            if (method != null) {
                method.releaseConnection();
            }
        }


    }
}
